import SplashComponent from "@components/Splash/SplashComponent";
import AppSettings from "@runtime/base/AppSettings";
import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";
import { Ref, ref, WatchStopHandle } from "@vue/runtime-dom";

/**
 * 起始页
 */
export default class HomeContent {
    private static instance: HomeContent = new HomeContent();
    /**
     * 起始页
     */
    public static get Default(): HomeContent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);

        for (const watchStopHandle of this.watchStopHandles) {
            watchStopHandle();
        }
        this.watchStopHandles.clear();
    }

    // 获取的任务
    public FaultPointCollection: Ref<any> = ref()
    public CacheFullData: Ref<any> = ref()
    // 区分运行启动还是开始任务启动
    public FaultPoints: Ref<any> = ref()
    // 时间
    public Duration: Ref<any> = ref();
    // 倒计时控制
    public timerControl: Ref<any> = ref()
    // 平台和本地返回和选择故障类页面是否显示
    public showType: Ref<boolean> = ref(false)

    public TypeStatus: Ref<boolean> = ref(false);
    public ApplicationType: Ref<string> = ref('');

    public Loaded: Ref<boolean> = ref(false);
    // public MultimeterData: Ref<ClientMessage.StructureCognitionResponse> = ref(new ClientMessage.StructureCognitionResponse)

    public BackView: Ref<string> = ref('');
    public FrontView: Ref<string> = ref('');
    public LeftView: Ref<string> = ref('');
    public RightView: Ref<string> = ref('');
    public Speed: Ref<number> = ref(0);
    public Gears: Ref<string> = ref('P');
    public ModeType: Ref<number> = ref(0);
    public RelaySequence: Ref<number> = ref(0)
    //遥控驾驶训练完成推送
    public NeolithTrainMessage:Ref<number> = ref(0)
    //遥控驾驶考核完成推送
    public NeolithExamMessage: Ref<any> = ref({})
    // 车辆启动提示
    public vehicleStart: Ref<any> = ref()
    //平行驾驶
    public ParallelDriving: Ref<any> = ref({});
    //列表数据
    public MenuList: Ref<any> = ref({})
    // 记录场景模型ID
    public ResourceID: Ref<Array<any>> = ref([])
    //Apo
    public ApoType: Ref<boolean> = ref(false)
    //UBANTU页面状态
    public UBANTUVal: Ref<number> = ref(0);
    //UBANTU启动
    public UBANTUType: Ref<number> = ref(0);
    //启动ubantu推送
    public PMUBANTU: Ref<number> = ref(0);
    //是否输入端口
    public WTIP: Ref<string> = ref('');
    //输入是否正确
    public InputContent: Ref<boolean> = ref(false)
    //类型是否正确
    public OptionTypeCorrect: Ref<boolean> = ref(false)
    //是否去除背景
    public REMBack: Ref<number> = ref(0);
    //是否显示浏览器
    public conTypes: Ref<boolean> = ref(false);
    public Boxerror: Ref<boolean> = ref(false);
    //Apollo车辆信息
    public Apo_Accelerator: Ref<number> = ref(0);
    public Apo_Angle: Ref<number> = ref(0);
    public Apo_Brake: Ref<number> = ref(0);
    public Apo_Speed: Ref<number> = ref(0);

    //成绩详情
    public ScoreDetails: Ref<any> = ref({})

    private watchStopHandles: Array<WatchStopHandle> = [];

    //load
    public ProgressText: Ref<string> = ref('');
    public ProgressVal: Ref<number> = ref(0);

    //存储终端输入信息
    public TerminalRecords: Ref<Array<any>> = ref([])
    public TerminalNnum: Ref<number> = ref(-1);

    // 产品名称
    public ProjectName: Ref<string> = ref('');

    public async Awake(): Promise<void> {
        this.Loaded.value = true;
        this.ProgressText.value = '渲染引擎初始化'
        this.ProgressVal.value = 90
        // 启动Unity
        // 资源编号
        const configuration = JSON.stringify({
            LocalAddress: AppSettings.Default.LocalAddress,
            ShortName: 'P10063',
            Type: 11,
        });

        await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationLaunchRequest('../EmbeddedApplication/Unity/Mix.Unity.exe', configuration));

        setTimeout(async () => {
            this.ProgressText.value = '环境模块初始化'
            this.ProgressVal.value = 75
        }, 800);

        // // 获取Unity版本状态
        await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.EmbeddedApplicationVersionRequest());

        setTimeout(async () => {
            this.ProgressText.value = '相机模块初始化'
            this.ProgressVal.value = 55
        }, 1200);

    }

    public async Destroy(): Promise<void> {
    }
}